package com.openweixin.sheknows.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.openweixin.sheknows.common.Pager;
import com.openweixin.sheknows.domain.Answer;
import com.openweixin.sheknows.service.AnswerService;
import com.openweixin.sheknows.util.SessionUtil;
@Controller
@RequestMapping("/answer")
public class AnswerController extends BaseController{

	@Autowired
	private AnswerService service;

	@RequestMapping
	public String toMain(){
		return "Answer";
	}

	@RequestMapping("/list")
	@ResponseBody 
	public Pager<Answer> list(Pager<Answer> pager){
		pager = service.findByPager(pager);
		return pager;
	}

	@RequestMapping("/add")
	public String add( Answer bean){
		Integer qid=bean.getQuestion().getQid();
		Integer uid=SessionUtil.getUserID();
		bean.setQuestion(null);
		service.answer(qid , uid, bean);
		return "redirect:../expert/questionListView.do"; 
	}

	@RequestMapping("/update")
	@ResponseBody 
	public String update( Answer bean){
		service.update(bean);
		return SUCCESS; 
	}

	@RequestMapping("/delete")
	@ResponseBody 
	public String delete(Integer[] ids){
		service.delete(ids);
		return SUCCESS; 
	}
	
	@RequestMapping("/setScore")
	@ResponseBody
	public String setScore( Integer id, Integer score){
		Integer uid=SessionUtil.getUserID();
		if( service.saveScore(id,  uid , score) )
			return SUCCESS;
		return FAIL;
	}

	@RequestMapping("/scoreView")
	public ModelAndView scoreView(@RequestParam Integer id){
		return new ModelAndView("answer/scoreView","answer",service.get(id));
	}
}
